// 坦克实体类
class Tank {
    constructor(id, playerId, x, y, width, height, direction, speed, health, color) {
        this.id = id;
        this.playerId = playerId;  // 坦克所属玩家ID
        this.x = x;
        this.y = y;
        this.width = width || 40;
        this.height = height || 40;
        this.direction = direction || 'up';
        this.speed = speed || 3;
        this.health = health || 100;
        this.color = color || '#00FF00';
        this.lastShootTime = 0;  // 上次射击时间
        this.active = true;  // 坦克是否有效
    }

    // 更新坦克位置
    update(input, deltaTime, walls, tanks) {
        if (!this.active || !input) return;
        
        const { up, down, left, right } = input;
        
        // 根据输入更新方向和位置
        let newX = this.x;
        let newY = this.y;
        
        if (up) {
            this.direction = 'up';
            newY -= this.speed;
        } else if (down) {
            this.direction = 'down';
            newY += this.speed;
        } else if (left) {
            this.direction = 'left';
            newX -= this.speed;
        } else if (right) {
            this.direction = 'right';
            newX += this.speed;
        }
        
        // 检测碰撞
        let collision = false;
        
        // 检查与墙壁的碰撞
        if (walls) {
            for (const wallId in walls) {
                const wall = walls[wallId];
                if (this.checkCollision({
                    x: newX,
                    y: newY,
                    width: this.width,
                    height: this.height
                }, wall)) {
                    collision = true;
                    break;
                }
            }
        }
        
        // 检查与其他坦克的碰撞
        if (!collision && tanks) {
            for (const tankId in tanks) {
                if (tankId !== this.id) {
                    const otherTank = tanks[tankId];
                    if (this.checkCollision({
                        x: newX,
                        y: newY,
                        width: this.width,
                        height: this.height
                    }, otherTank)) {
                        collision = true;
                        break;
                    }
                }
            }
        }
        
        // 如果没有碰撞，更新位置
        if (!collision) {
            this.x = newX;
            this.y = newY;
        }
    }

    // 渲染坦克
    render(ctx) {
        if (!this.active) return;
        
        // 获取渲染系统的偏移量
        const offsetX = ctx.canvas.offsetX || 0;
        const offsetY = ctx.canvas.offsetY || 0;
        
        // 绘制坦克主体
        ctx.fillStyle = this.color;
        ctx.fillRect(this.x + offsetX, this.y + offsetY, this.width, this.height);
        
        // 绘制坦克炮管
        ctx.fillStyle = '#333333';
        let barrelX, barrelY, barrelWidth, barrelHeight;
        
        switch (this.direction) {
            case 'up':
                barrelWidth = this.width / 4;
                barrelHeight = this.height / 2;
                barrelX = this.x + offsetX + (this.width - barrelWidth) / 2;
                barrelY = this.y + offsetY - barrelHeight / 2;
                break;
            case 'down':
                barrelWidth = this.width / 4;
                barrelHeight = this.height / 2;
                barrelX = this.x + offsetX + (this.width - barrelWidth) / 2;
                barrelY = this.y + offsetY + this.height - barrelHeight / 2;
                break;
            case 'left':
                barrelWidth = this.width / 2;
                barrelHeight = this.height / 4;
                barrelX = this.x + offsetX - barrelWidth / 2;
                barrelY = this.y + offsetY + (this.height - barrelHeight) / 2;
                break;
            case 'right':
                barrelWidth = this.width / 2;
                barrelHeight = this.height / 4;
                barrelX = this.x + offsetX + this.width - barrelWidth / 2;
                barrelY = this.y + offsetY + (this.height - barrelHeight) / 2;
                break;
        }
        
        ctx.fillRect(barrelX, barrelY, barrelWidth, barrelHeight);
        
        // 绘制生命值
        const healthBarWidth = this.width;
        const healthBarHeight = 5;
        const healthBarX = this.x + offsetX;
        const healthBarY = this.y + offsetY - 10;
        
        // 背景
        ctx.fillStyle = '#333333';
        ctx.fillRect(healthBarX, healthBarY, healthBarWidth, healthBarHeight);
        
        // 生命值
        const healthPercent = this.health / 100;
        ctx.fillStyle = healthPercent > 0.5 ? '#00FF00' : healthPercent > 0.25 ? '#FFFF00' : '#FF0000';
        ctx.fillRect(healthBarX, healthBarY, healthBarWidth * healthPercent, healthBarHeight);
    }

    // 射击
    shoot(bulletSpeed) {
        if (!this.active) return null;
        
        const now = Date.now();
        if (now - this.lastShootTime < 500) return null; // 射击冷却时间
        
        this.lastShootTime = now;
        
        let bulletX = this.x + this.width / 2 - 5;
        let bulletY = this.y + this.height / 2 - 5;
        let velocityX = 0;
        let velocityY = 0;
        
        switch (this.direction) {
            case 'up':
                bulletY = this.y - 10;
                velocityY = -bulletSpeed;
                break;
            case 'down':
                bulletY = this.y + this.height;
                velocityY = bulletSpeed;
                break;
            case 'left':
                bulletX = this.x - 10;
                velocityX = -bulletSpeed;
                break;
            case 'right':
                bulletX = this.x + this.width;
                velocityX = bulletSpeed;
                break;
        }
        
        return {
            tankId: this.id,
            playerId: this.playerId,
            x: bulletX,
            y: bulletY,
            velocityX,
            velocityY,
            color: this.color
        };
    }

    // 碰撞检测
    checkCollision(obj1, obj2) {
        return (
            obj1.x < obj2.x + obj2.width &&
            obj1.x + obj1.width > obj2.x &&
            obj1.y < obj2.y + obj2.height &&
            obj1.y + obj1.height > obj2.y
        );
    }

    // 受到伤害
    takeDamage(damage) {
        if (!this.active) return;
        
        this.health -= damage;
        if (this.health <= 0) {
            this.destroy();
        }
    }

    // 销毁坦克
    destroy() {
        this.active = false;
    }
}

// 如果在浏览器环境中，将Tank添加到window对象
if (typeof window !== 'undefined') {
    window.Tank = Tank;
}

// 如果在Node.js环境中，导出Tank类
if (typeof module !== 'undefined' && module.exports) {
    module.exports = Tank;
}